<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备id" prop="did">
      <el-input v-model="dataForm.did" placeholder="设备id"></el-input>
    </el-form-item>
    <el-form-item label="设备名称" prop="devName">
      <el-input v-model="dataForm.devName" placeholder="设备名称"></el-input>
    </el-form-item>
    <el-form-item label="测点id" prop="tid">
      <el-input v-model="dataForm.tid" placeholder="测点id"></el-input>
    </el-form-item>
    <el-form-item label="测点名称" prop="tagName">
      <el-input v-model="dataForm.tagName" placeholder="测点名称"></el-input>
    </el-form-item>
    <el-form-item label="告警类型 ("alarm"/告警产生"recover"/告警恢复)" prop="type">
      <el-input v-model="dataForm.type" placeholder="告警类型 ("alarm"/告警产生"recover"/告警恢复)"></el-input>
    </el-form-item>
    <el-form-item label="告警内容" prop="alarmText">
      <el-input v-model="dataForm.alarmText" placeholder="告警内容"></el-input>
    </el-form-item>
    <el-form-item label="推送时间" prop="timeStamp">
      <el-input v-model="dataForm.timeStamp" placeholder="推送时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/hik/cdcalarm'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          alarmId: 0,
          did: '',

          devName: '',

          tid: '',

          tagName: '',

          type: '',

          alarmText: '',

          timeStamp: '',

          createTime: '',

          updateTime: '',

          updateBy: '',

          delFlag: '',

        },
        dataRule: {
          did: [
            { required: true, message: '设备id不能为空', trigger: 'blur' }
          ],

          devName: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],

          tid: [
            { required: true, message: '测点id不能为空', trigger: 'blur' }
          ],

          tagName: [
            { required: true, message: '测点名称不能为空', trigger: 'blur' }
          ],

          type: [
            { required: true, message: '告警类型 ("alarm"/告警产生"recover"/告警恢复)不能为空', trigger: 'blur' }
          ],

          alarmText: [
            { required: true, message: '告警内容不能为空', trigger: 'blur' }
          ],

          timeStamp: [
            { required: true, message: '推送时间不能为空', trigger: 'blur' }
          ],

          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],

          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],

          updateBy: [
            { required: true, message: '操作人不能为空', trigger: 'blur' }
          ],

          delFlag: [
            { required: true, message: '0 正常 1或者其他删除不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.alarmId = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.alarmId) {
            getObj(this.dataForm.alarmId).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.alarmId) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
